package com.ljkj.cordial.chat.dao;

import android.text.TextUtils;

import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.util.contactList.PinYinSortBean;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Property;

import java.util.List;

/**
 * 用户信息表
 */
@Entity(nameInDb = "UserTableBean")
public class UserTableBean extends PinYinSortBean {
    public static String TYPE_VISITOR = IMSContacts.UserType.TOURISTS_USER;
    public static String TYPE_REGISTER = IMSContacts.UserType.REGISTERED_USER;
    public static int TYPE_CREATOR = 0;
    public static int TYPE_FRIEND = 1;
    @Id(autoincrement = true)
    private Long id;
    @Property(nameInDb = "USER_TYPE")
    private String userType;
    // 注册用户ID
    @Index(unique = true)
    @Property(nameInDb = "USER_ID")
    private String userId;
    // 游客用户ID
    @Property(nameInDb = "TEMPORARY_ID")
    private String temporaryId;
    /**
     * 0创建者1好友
     */
    @Property(nameInDb = "CREATOR_STATE")
    private int createState;
    // 密码
    @Property(nameInDb = "SECRET")
    private String secret;

    // 注册用户昵称
    @Property(nameInDb = "NICKNAME")
    private String nickname;

    // 注册用户手机号码
    @Property(nameInDb = "PHONE")
    private String phone;

    // 头像
    @Property(nameInDb = "AVATAR")
    private String avatar;
    @Property(nameInDb = "AVATAR_ID")
    private String avatarId;
    @Property(nameInDb = "AVATAR_PATH")
    private String avatarPath;
    // 验证码
    @Property(nameInDb = "CAPTCHA")
    private String captcha;

    @Generated(hash = 2111220440)
    public UserTableBean(Long id, String userType, String userId, String temporaryId, int createState, String secret, String nickname, String phone, String avatar, String avatarId,
                         String avatarPath, String captcha) {
        this.id = id;
        this.userType = userType;
        this.userId = userId;
        this.temporaryId = temporaryId;
        this.createState = createState;
        this.secret = secret;
        this.nickname = nickname;
        this.phone = phone;
        this.avatar = avatar;
        this.avatarId = avatarId;
        this.avatarPath = avatarPath;
        this.captcha = captcha;
    }

    @Generated(hash = 666752163)
    public UserTableBean() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserType() {
        return this.userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getUserId() {
        return this.userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getTemporaryId() {
        return this.temporaryId;
    }

    public void setTemporaryId(String temporaryId) {
        this.temporaryId = temporaryId;
    }

    public String getSecret() {
        return this.secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getNickname() {
        return this.nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAvatar() {
        return this.avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getCaptcha() {
        return this.captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public void insert(UserDto data) {
        setUserType(data.getUserType());
        if (TYPE_VISITOR.equals(data.getUserType())) {
            setTemporaryId(data.getUserId());
        } else {
            setUserId(data.getUserId());
        }
        setCreateState(TYPE_CREATOR);
        setAvatar(data.getAvatar());
        setPhone(data.getPhone());
        if (!TextUtils.isEmpty(data.getSecret())) {
            setSecret(data.getSecret());
        }
        if (!TextUtils.isEmpty(data.getNickname())) {
            setNickname(data.getNickname());
        }
        Long id = getIdUseExist(data.getUserType(), data.getUserId());
        if (id != 0L) {
            setId(id);
        }
        DbController.getInstance().getWrite().getUserTableBeanDao().insertOrReplace(this);
        ChatApp.getInstance().put(this);
    }

    public static void update(UserTableBean user) {
        DbController.getInstance().getWrite().getUserTableBeanDao().save(user);
        ChatApp.getInstance().put(user);
    }

    public void insert(RegisteredUserDto data) {
        setUserType(TYPE_REGISTER);
        setUserId(data.getUserId());
        setAvatar(data.getAvatar());
        setPhone(data.getPhone());
        if (!TextUtils.isEmpty(data.getSecret())) {
            setSecret(data.getSecret());
        }
        if (!TextUtils.isEmpty(data.getNickname())) {
            setNickname(data.getNickname());
        }
        setCaptcha(data.getCaptcha());
        setAvatar(data.getAvatar());
        setCreateState(TYPE_CREATOR);
        Long id = getIdUseExist(TYPE_REGISTER, data.getUserId());
        if (id != 0L) {
            setId(id);
        }
        DbController.getInstance().getWrite().getUserTableBeanDao().insertOrReplace(this);
        ChatApp.getInstance().put(this);
    }


    public static UserTableBean queryUserById(String userType, String userId) {
        if (TYPE_VISITOR.equals(userType)) {
            return DbController.getInstance().getRead().getUserTableBeanDao().queryBuilder().where(UserTableBeanDao.Properties.TemporaryId.eq(userId)).unique();
        }
        return DbController.getInstance().getRead().getUserTableBeanDao().queryBuilder().where(UserTableBeanDao.Properties.UserId.eq(userId)).unique();

    }

    public static UserTableBean getCurrentUser() {
        if (TYPE_VISITOR.equals(ChatApp.getInstance().getUserType())) {
            return DbController.getInstance().getRead().getUserTableBeanDao().queryBuilder().where(UserTableBeanDao.Properties.TemporaryId.eq(ChatApp.getInstance().getTouristsId())).unique();
        }
        return DbController.getInstance().getRead().getUserTableBeanDao().queryBuilder().where(UserTableBeanDao.Properties.UserId.eq(ChatApp.getInstance().getRegisterId())).unique();

    }

    public static boolean isUseExist(String userType, String userId) {
        if (TYPE_VISITOR.equals(userType)) {
            UserTableBean b = DbController.getInstance().getRead()
                    .getUserTableBeanDao()
                    .queryBuilder().where(UserTableBeanDao.Properties.TemporaryId.eq(userId)).unique();
            return b != null;
        } else {
            UserTableBean b = DbController.getInstance().getRead()
                    .getUserTableBeanDao()
                    .queryBuilder().where(UserTableBeanDao.Properties.UserId.eq(userId)).unique();
            return b != null;
        }

    }

    /**
     * 创建者列表
     *
     * @return
     */
    public static List<UserTableBean> getCreatorList() {
        return DbController.getInstance().getRead().getUserTableBeanDao().queryBuilder()
                .where(UserTableBeanDao.Properties.CreateState.eq(UserTableBean.TYPE_CREATOR)).list();
    }

    public static Long getIdUseExist(String userType, String userId) {
        if (TYPE_VISITOR.equals(userType)) {
            UserTableBean b = DbController.getInstance().getRead()
                    .getUserTableBeanDao()
                    .queryBuilder().where(UserTableBeanDao.Properties.TemporaryId.eq(userId)).unique();
            return b != null ? b.getId() : 0L;
        } else {
            UserTableBean b = DbController.getInstance().getRead()
                    .getUserTableBeanDao()
                    .queryBuilder().where(UserTableBeanDao.Properties.UserId.eq(userId)).unique();
            return b != null ? b.getId() : 0L;
        }

    }

    public static void deleteByUserId(String userId) {
        if (TextUtils.isEmpty(userId)) {
            return;
        }
        UserTableBean b = DbController.getInstance().getRead()
                .getUserTableBeanDao()
                .queryBuilder().whereOr(UserTableBeanDao.Properties.TemporaryId.eq(userId)
                        , UserTableBeanDao.Properties.UserId.eq(userId)).unique();
        if (b != null) {
            DbController.getInstance().getWrite().delete(b);
        }
    }

    @Override
    public String sortName() {
        return nickname;
    }

    public String getAvatarId() {
        return this.avatarId;
    }

    public void setAvatarId(String avatarId) {
        this.avatarId = avatarId;
    }

    public int getCreateState() {
        return this.createState;
    }

    public void setCreateState(int createState) {
        this.createState = createState;
    }

    public String getAvatarPath() {
        return this.avatarPath;
    }

    public void setAvatarPath(String avatarPath) {
        this.avatarPath = avatarPath;
    }
}
